﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Threading;
using SmartBoard.BackGround.BoardElement;
using SmartBoard.BackGround.Data;
using SmartBoard.Background.Dialogs;
using SmartBoard.Core.Command;

namespace SmartBoard.Background.Commands
{
    public class ChangeBackgroundCommand : UIBinderRemoteCommandBase
    {
        private readonly BackGroundBoardElement _owner;

        public ChangeBackgroundCommand(BackGroundBoardElement owner)
        {
            _owner = owner;
            this.Descriptor = new CommandDescriptor("Change Background", BoardCommandManager.CHANGE_BACKGROUND_COMMAND, this);
        }

        #region ICommand Members

        protected override bool PrepareData(object input, out object output)
        {
            var dialog = (IChangeBackgroundDialog)DialogManager.Instance.GetDialog(typeof(IChangeBackgroundDialog));
            dialog.SelectedColor = ((SolidColorBrush)_owner.Background).Color;
            if (dialog.ShowModal() == DialogResult.OK)
            {
                output = new V10BackgroundData() { A = dialog.SelectedColor.A, R = dialog.SelectedColor.R, G = dialog.SelectedColor.G, B = dialog.SelectedColor.B };
                return true;
            }
            output = input;
            return false;
        }

        protected override void DoExecution(object parameter)
        {
            var data = parameter as V10BackgroundData;
            if (data != null)
            {
                _owner.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)
                                              (() =>
                                                   {
                                                       _owner.Background = new SolidColorBrush(Color.FromArgb(data.A, data.R, data.G, data.B));
                                                   }));
            }
        }

        protected override int GetCommandId()
        {
            return this.Descriptor.CommandId;
        }
        #endregion
    }
}